package com.acmen.easyapi.executor;

import com.acmen.easyapi.enums.BasicCode;
import com.acmen.easyapi.model.Response;
import com.acmen.easyapi.repository.ApiRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 接口删除执行器
 */
@Component
public class ApiDeleteExe {

    @Autowired
    private ApiRepository apiRepository;

    @Value("${cmp.api.intranetIp:*}")
    private String intranetIp;

    /**
     * 删除接口
     * @param ip IP地址
     * @param apiIds 接口ID列表
     * @return 响应对象
     */
    public Response delApi(String ip, List<String> apiIds) {
        // 检查IP地址是否合法
        if (!(ip.matches(intranetIp.replace("*", ".*")) || ip.startsWith("127.0."))) {
            return Response.failure(BasicCode.FORBIDDEN.getCode(), "不允许操作！");
        }

        // 批量删除接口
        apiRepository.batchDeleteWithVersion(apiIds);

        return Response.success();
    }
}
